package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.entity.hardware.Printer;
import com.xishu.entity.hardware.PrinterLabel;
import com.xishu.entity.shop.Item;
import com.xishu.entity.shop.Shop;
import com.xishu.response.ResponseData;
import com.xishu.response.ResponseStatus;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.ItemService;
import com.xishu.service.print.PrinterService;
import com.xishu.service.ShopService;
import com.xishu.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 打印标签设置
 */
@RestController
@Api(description = "打印标签设置")
public class PrinterLabelController implements Tools {
    private static Logger logger = LoggerFactory.getLogger(PrinterLabelController.class);
    private CommonService commonService = CommonService.getInstance();

    @ApiOperation(value = "创建打印标签")
    @PostMapping(value = ("/user/printer/label"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true, checkShopId = true)
    public ResponseData create(@RequestBody PrinterLabel printerLabel) throws Exception {
        VerifyUtil.verify(() -> isNotEmpty(printerLabel.getPrinterLabel()));
        VerifyUtil.verify(() -> printerLabel.getShopId() != null);

        return CommonService.getInstance().createObject(printerLabel);
    }

    @ApiOperation(value = "修改打印标签")
    @PutMapping(value = ("/user/printer/label/modify"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true)
    public ResponseData modify(@RequestBody PrinterLabel printerLabel) throws Exception {
        VerifyUtil.verify(() -> isNotEmpty(printerLabel.getPrinterLabel()));
        PrinterLabel printerLabelInDb = commonService.findEntity(printerLabel.getId(), PrinterLabel.class);
        String oldPrinterLabel = printerLabelInDb.getPrinterLabel();
        Long shopId = printerLabelInDb.getShopId();

        return CommonService.getInstance().updateObject(printerLabel, label -> {
            try {
                //如果修改名称，修改菜品里面的打印标签
                List<Item> itemList = ItemService.getInstance().findItemByShopId(shopId);
                for (Item item : itemList) {
                    if (equals(item.getPrintLabel(), oldPrinterLabel)) {
                        item.setPrintLabel(printerLabel.getPrinterLabel());
                        commonService.save(item);
                    }
                }

                //修改打印机里面的标签
                List<Printer> printerList = ShopService.getInstance().findAllShopPrinters(shopId);
                for (Printer printer : printerList) {
                    List<String> printerLabelList = printer.getPrinterLabelList();
                    if (isNotEmpty(printerLabelList) && printerLabelList.contains(oldPrinterLabel)) {
                        printerLabelList.remove(oldPrinterLabel);
                        printerLabelList.add(printerLabel.getPrinterLabel());
                        commonService.save(printer);
                    }
                }
            } catch (Exception e) {
                logger.error("e", e);
            }
        });
    }

    @ApiOperation(value = "查询打印标签")
    @PutMapping(value = ("/user/printer/label"), produces = ("application/json;charset=UTF-8"))
    public ResponseData search(@RequestBody PrinterLabel printerLabel) throws Exception {
        return CommonService.getInstance().searchResponse(printerLabel, (p) -> {
            try {
                Long shopId = p.getShopId();
                Shop shop = ShopService.getInstance().findShopById(shopId);
                p.setShopName_zh(shop.getName_zh());
                p.setShopName_en(shop.getName_en());
            } catch (Exception e) {
                logger.error("e", e);
            }
        });
    }

    @ApiOperation(value = "删除打印标签")
    @DeleteMapping(value = ("/user/printer/label"), produces = ("application/json;charset=UTF-8"))
    public ResponseData delete(@RequestBody PrinterLabel printerLabel) throws Exception {
        //如果有关联的菜品，不能删除
        PrinterLabel printerLabelInDb = commonService.findEntity(printerLabel.getId(), PrinterLabel.class);
        VerifyUtil.verify(() -> printerLabelInDb != null);
        Long shopId = printerLabelInDb.getShopId();
        List<Item> itemList = ItemService.getInstance().findItemByShopId(shopId);

        for (Item item : itemList) {
            if (equals(item.getPrintLabel(), printerLabelInDb.getPrinterLabel())) {
                VerifyUtil.throwError(ResponseStatus.PRINTER_LABEL_RELATED_TO_ITEM);
            }
        }

        return CommonService.getInstance().delete(printerLabel);
    }

}
